package com.web.manager;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.web.domain.ApiResponse;
import com.web.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author FDjavaone
 * @date 2017/8/23
 */
public class ExpressManager {


    public static ApiResponse getExpress(String expressNumber){
        ApiResponse ponse=new ApiResponse();
        String host = "http://jisukdcx.market.alicloudapi.com";
        String path = "/express/query";
        String method = "GET";
        String appcode = "2367dde9fcb5418c86b13a0bd8a3e7f6";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("number",expressNumber);
        querys.put("type", "auto");


        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            String resultBody=EntityUtils.toString(response.getEntity());
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode rootNode;
            rootNode = objectMapper.readValue(resultBody, JsonNode.class);
            String resultCode = rootNode.get("status").asText();
            // 成功
            if ("0".equals(resultCode)) {
                JsonNode resultNode=rootNode.get("result");
                JsonNode list=resultNode.get("list");
                ponse.setParams(list);
                ponse.setStatus(900000);
                ponse.setMessage("物流信息查询成功");

            }else if("201".equals(resultCode)){
                //快递单号为空
                ponse.setStatus(900120);
                ponse.setMessage("快递单号为空");
            }else if("202".equals(resultCode)){
                //快递公司为空
                ponse.setStatus(900121);
                ponse.setMessage("快递公司为空");
            }else if("203".equals(resultCode)){
                //快递公司不存在
                ponse.setStatus(900123);
                ponse.setMessage("快递公司不存在");
            }else if("204".equals(resultCode)){
                //快递公司识别失败
                ponse.setStatus(900124);
                ponse.setMessage("快递公司识别失败");
            }else{
                ponse.setStatus(900125);
                ponse.setMessage("没有信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ponse;
    }

}
